﻿using System;
using System.IO;
using System.IO.Compression;
namespace Moonlite.ExtensionApi
{
    public class CompressionUtils
    {
        public static void Compress(FileInfo info, string newFilePath)
        {
            using (FileStream inFile = info.OpenRead())
            {
                if ((File.GetAttributes(info.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & info.FullName != newFilePath)
                {
                    using (FileStream outFile = File.Create(newFilePath))
                    {
                        using (GZipStream compress = new GZipStream(outFile, CompressionMode.Compress))
                        {
                            inFile.CopyTo(compress);
                        }
                    }
                }
            }
        }

        public static void Decompress(FileInfo info)
        {
            using (FileStream inFile = info.OpenRead())
            {
                string curFile = info.FullName + ".temp";
                using (FileStream outFile = File.Create(curFile))
                {
                    using (GZipStream decompress = new GZipStream(inFile, CompressionMode.Decompress))
                    {
                        decompress.CopyTo(outFile);
                    }
                }
            }
        }
    }
}
